package com.e3roid.examples;

import android.graphics.Color;
import android.graphics.Typeface;
import android.view.MotionEvent;
import android.widget.Toast;

import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.sprite.TextSprite;

public class LabelExample extends E3Activity {

	private final static int WIDTH  = 320;
	private final static int HEIGHT = 480;
	
	private TextSprite label;

	@Override
	public E3Engine onLoadEngine() {
		E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
		engine.requestFullScreen();
		engine.requestPortrait();
		return engine;
	}

	@Override
	public E3Scene onLoadScene() {
		E3Scene scene = new E3Scene();
		scene.addEventListener(this);
		scene.setBackgroundColor(0.94f, 1.00f, 0.94f, 1);
		
		// Move the label to center of the screen.
		// Label width and height cannot be determined before loading Label object.
		// So if you want to move the label relative to the label size, use move() method
		// after creating Label instance.
		// Use getWidth() to get screen width, getHeight() to get screen height
		// because width and height can be changed depending on the screen resolution policy.
		label.move((getWidth() - label.getWidth()) / 2, (getHeight() - label.getHeight()) / 2);
		
		scene.getTopLayer().add(label);
		Toast.makeText(this, "Touch screen to move the label.", Toast.LENGTH_LONG).show();
		return scene;
	}

	@Override
	public void onLoadResources() {
		// Create new Label instance with parameters
		// fontSize is 18, foreground color is blue, background color is gray.
		label = new TextSprite("The quick fox and a lazy dog.", 18, Color.BLUE, Color.GRAY, Typeface.DEFAULT, this);
	}
	
	@Override
	public boolean onSceneTouchEvent(E3Scene scene, MotionEvent motionEvent) {
		if (label != null) {
			if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
				// x and y must be adjusted to scene's screen position because
				// MotionEvent's getX()&getY() returns pixel position of the actual device.
				int x = getTouchEventX(scene, motionEvent) - (label.getWidth()  / 2);
				int y = getTouchEventY(scene, motionEvent) - (label.getHeight() / 2);
				label.move(x, y);
			}
		}
		return false;
	}
}